package org.ethan.pet.service.impl;


import org.ethan.basic.exception.BasicException;
import org.ethan.basic.query.BaseQuery;
import org.ethan.basic.service.impl.BaseServiceImpl;
import org.ethan.basic.util.PageList;
import org.ethan.pet.domain.Pet;
import org.ethan.pet.mapper.PetDetailMapper;
import org.ethan.pet.mapper.PetMapper;
import org.ethan.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    /**
     * 上架下架的宠物
     * 1.判断id是否为空
     * 2.只有下架的宠物才能上架
     * 3.修改下架状态为上架
     * @param ids 宠物id集合
     */
    @Override
    public void onSale(List<Long> ids) {
        // 1.判断id是否为空
        if (ids == null || ids.size() == 0) {
            throw new BasicException("请求异常！！");
        }

        /*
         *
         * update t_pet set ${timeField} = now(), state = #{state} where id in
         *         <foreach collection="ids" separator="," open="(" close=")" item="id">
         *             #{id}
         *         </foreach>
         * @param timeField 传入上架或者下架的时间
         * @param state 更改状态
         * @param ids 要更改的数据的id
         */
        petMapper.updateState("onsaletime", Pet.STATE_ONSALE, ids);

    }

     /**
     * 上架下架的宠物
     * 1.判断id是否为空
     * 2.只有下架的宠物才能上架
     * 3.修改下架状态为上架
     * @param ids 宠物id集合
     */
    @Override
    public void offSale(List<Long> ids) {
        // 1.判断id是否为空
        if (ids == null || ids.size() == 0) {
            throw new BasicException("请求异常！！");
        }

        /*
         * update t_pet set ${timeField} = now(), state = #{state} where id in
         *         <foreach collection="ids" separator="," open="(" close=")" item="id">
         *             #{id}
         *         </foreach>
         * @param timeField 传入上架或者下架的时间
         * @param state 更改状态
         * @param ids 要更改的数据的id
         */
        petMapper.updateState("offsaletime", Pet.STATE_OFFSALE, ids);

    }

    /**
     * 根据id获取该宠物的详细信息
     * @param id id
     * @return pet
     */
    @Override
    public Pet findOnsalePetById(Long id) {
        return petMapper.findByIdAndState(id, Pet.STATE_ONSALE);
    }


}
